package com.netease.dragonwar.sprite;

import java.awt.image.BufferedImage;

import com.netease.dragonwar.common.Constant;

/**
 * @author louxj424
 * @date 创建时间：2018年3月5日 上午10:22:31
 * @version 0.1.0
 * @since jdk 1.7.0
 */
public abstract class Sprite {
	/**
	 * 中心点X坐标
	 */
	protected int x;

	/**
	 * 中心点Y坐标
	 */
	protected int y;

	/**
	 * 宽度
	 */
	protected int width;

	/**
	 * 高度
	 */
	protected int height;

	/**
	 * 移动速度
	 */
	protected int speed;

	/**
	 * 含参的构造函数
	 * @param x
	 * @param y
	 * @param width
	 * @param height
	 * @param speed
	 */
	public Sprite(int x, int y, int width, int height, int speed) {
		this.x = x;
		this.y = y;
		this.width = width;
		this.height = height;
		this.speed = speed;
	}

	public int getX() {
		return x;
	}

	public void setX(int x) {
		this.x = x;
	}

	public int getY() {
		return y;
	}

	public void setY(int y) {
		this.y = y;
	}

	public int getWidth() {
		return width;
	}

	public void setWidth(int width) {
		this.width = width;
	}

	public int getHeight() {
		return height;
	}

	public void setHeight(int height) {
		this.height = height;
	}

	public int getSpeed() {
		return speed;
	}

	public void setSpeed(int speed) {
		this.speed = speed;
	}

	/**
	 * 抽象的移动的方法
	 */
	public abstract void move();

	/**
	 * 抽象的获取图片的方法
	 * @return
	 */
	public abstract BufferedImage getImage();

	/**
	 * 是否越界
	 * @return
	 */
	public boolean isOutside() {
		if (getX() + getWidth() / 2 < 0) {
			return true;
		} else if (getX() - getWidth() / 2 > Constant.WINDOW_WIDTH) {
			return true;
		} else if (getY() + getHeight() / 2 < 0) {
			return true;
		} else if (getY() - getHeight() / 2 > Constant.WINDOW_HEIGHT) {
			return true;
		}

		return false;
	}

	// 碰撞检测
	public boolean isHit(Sprite target) {
		if (null == target) {
			return false;
		}
		int distcenX = Math.abs(this.getX() - target.getX());
		int distcenY = Math.abs(this.getY() - target.getY());
		int adjust = this.getSafeDistanceAdjustValue();
		int safeX = this.getWidth() / 2 + target.getWidth() / 2 + adjust / 2;
		int safeY = this.getHeight() / 2 + target.getHeight() / 2 + adjust / 2;

		if (distcenX < safeX && distcenY < safeY) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 安全距离的附加值（适当的正值）或图片边距造成的偏移量（适当的负值）
	 * @return
	 */
	protected int getSafeDistanceAdjustValue() {
		return -20;
	}

}
